/*****************************************************************************
* file  : test.c
* creat : apleilx
* data  : 2017/03/02
*******************************************************************************/
#define   __os_obj_creat__                                        // define objects in rtxapp module
#include "os_obj.h"                                          // RTOS object definitions
#include "common.h"
#include "bsp_api.h"

void task_system (void  const *argument);
void modbus_task (void const *argument);

//独立堆栈 需定义
osThreadDef(task_system, osPriorityHigh, 1, 0x400);
osThreadDef(modbus_task, osPriorityNormal, 1, 0x400);


osMutexDef(eep_mutex);
osMutexDef(q25_mutex);
osMutexDef(printf_mutex);
osMutexDef(malloc_mutex);
osMutexDef(file_mutex);

/*----------------------------------------------------------------------------
 *      systask , create befor os start
 *---------------------------------------------------------------------------*/
int thread_init (void)
{
    //系统任务创建
    //系统对象创建
	os_obj.mid_file = osMutexCreate(osMutex(file_mutex));
    os_obj.mid_printf = osMutexCreate(osMutex(printf_mutex));    
    os_obj.mid_malloc = osMutexCreate(osMutex(malloc_mutex));  
    
    os_obj.tid_sys_task = osThreadCreate(osThread(task_system),NULL);
    
    return(0);
}

/*----------------------------------------------------------------------------
 *   created in systask
 *---------------------------------------------------------------------------*/

int thread_create (void)
{
    //任务创建
    os_obj.tid_mb_task = osThreadCreate(osThread(modbus_task),NULL);

    return(0);
}

/*----------------------------------------------------------------------------
 *   os lock
 *---------------------------------------------------------------------------*/
void os_lock(void)
{
    osKernelLock();
}

void os_unlock(void)
{
    osKernelUnlock();
}

/*----------------------------------------------------------------------------
 *   task info print
 *---------------------------------------------------------------------------*/
void os_task_info_print(osThreadId tid, const char *name)
{
	int stk, stk_used, pused, prio;

	if(!tid)
		return;
	
	stk =  osThreadGetStackSize(tid);
	stk_used = stk - osThreadGetStackSpace(tid);
	pused = stk_used * 100 / stk;
	prio = osThreadGetPriority(tid);
	
	dbg_print("stack size: %05x, stack used: %5x, <%02d%%>, prio : %2d, name: %s\n", 
		stk, 
		stk_used, 
		pused,
		prio,
		name
	);
	
	(void) pused;
	(void) prio;
}

void os_task_info(void)
{
	dbg_print("system stack used : %d %%\n", stack_used()/10);
	
	dbg_print("task amount : %d \n", osThreadGetCount());
	
	os_task_info_print(os_obj.tid_sys_task, "sys task");
	os_task_info_print(os_obj.tid_gui_task, "gui task");
	os_task_info_print(os_obj.tid_key_task, "key task");
	os_task_info_print(os_obj.tid_console_task, "console task");
	os_task_info_print(os_obj.tid_flash_task, "flash task");
	os_task_info_print(os_obj.tid_mb_task, "modbus task");
	os_task_info_print(os_obj.tid_sd_task, "sd task");
	os_task_info_print(os_obj.tid_str_task, "str task");
	os_task_info_print(os_obj.tid_crypto, "crypto task");
	os_task_info_print(os_obj.tid_usb_task, "usb task");
}
